package com.yufei.infoExtractor.extractor.fetchdecision;

import java.util.List;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yufei.infoExtractor.pfw.entity.Seedsite;
import com.yufei.infoExtractor.util.AppUtil;
import com.yufei.infoExtractor.util.CommonUtil;

public class DecideByUrlSimilarity implements FetchDecisionMaker {
	private static final Log mLog = LogFactory.getLog(DecideByUrlSimilarity.class);

	@Override
	public boolean makeDecision(Map context, String currentUrlStr) {
		// TODO Auto-generated method stub
		double similartity = 0.00;
		double passedSimilartity = 0.00;
		Seedsite seedsite = (Seedsite) context.get("seed");
		passedSimilartity = seedsite.getPassedSimilarityValue();
		List<String> targetUrls = seedsite.getLinkSwatches();
		if (targetUrls == null || targetUrls.size() == 0) {
			throw new RuntimeException("the linkSwatches config is needed!");
		}
		if (passedSimilartity == 0.00) {
			// get the global value
			passedSimilartity = AppUtil.getInfoExtractorConfig()
					.getPassedSimilarityValue();
		}
		boolean isOk = false;
		for (String targetUrl : targetUrls) {
			List<Integer>[] vectorofString = CommonUtil.getVectorofString(
					targetUrl, currentUrlStr);
			similartity = CommonUtil.calculateSimilartityByInteger(vectorofString[0],
					vectorofString[1]);
			if (similartity >= passedSimilartity) {
				isOk = true;
				break;
	
			}
			mLog.debug("链接'"+currentUrlStr+"'不符合目标链接特征，很可能为不相干链接，此次处理直接忽略");
	
		}
		return isOk;
	}


}
